﻿define(['ajax'], function (ajax) {
    "use strict";

    var exports = {},
         ViewModel;

    ViewModel = function () {
        var self = this;

        var ticketDataViewModel = {
            timet: ko.observable(),
            timev: ko.observable(),
        };

        var tickets;

        //Function to get data using signalR from Hub methods.
        $(function () {
            tickets = $.connection.memoryUsageHub;
            tickets.client.updateMessages = function (data) { ticketDataViewModel.timet(data); };
            tickets.client.updateVirtualMemoryData = function (data) { ticketDataViewModel.timev(data); };
            tickets.client.checkIntenetConnection = function (data) { ticketDataViewModel.timev(data); };
            $.connection.hub.start();
            $.connection.hub.stateChanged(function (state) {                
                if (state.newState === $.signalR.connectionState.connected) {
                    tickets.server.physicalMemoryState();
                    setInterval(updatetime, 3000);
                }
            });
            function updatetime() {
                tickets.server.physicalMemoryState();
                tickets.server.virtualMemoryState();
            }
        });

        //Guage Section
        var physicalMemory = new JustGage({
            id: "PM",
            value: 100,
            min: 0,
            max: 200,
            title: "Physical memory",
            label: "MB",
            startAnimationTime: 2000,
            startAnimationType: ">",
            refreshAnimationTime: 1000,
            refreshAnimationType: "bounce"
        });
        var virtualMemory = new JustGage({
            id: "VM",
            value: 100,
            min: 0,
            max: 1000,
            title: "Virtual memory",
            label: "MB",
            startAnimationTime: 2000,
            startAnimationType: ">",
            refreshAnimationTime: 1000,
            refreshAnimationType: "bounce"
        });
        setInterval(function () {
            var physicalMemoryValue = Math.ceil((ticketDataViewModel.timet() / 1024) / 1024);
            var virtualMemoryValue = Math.ceil((ticketDataViewModel.timev() / 1024) / 1024);
            physicalMemory.refresh(physicalMemoryValue);
            virtualMemory.refresh(virtualMemoryValue);
        }, 3000);

        self.virtualMemoryUsageData = ko.observableArray([]);
        self.virtualMemoryUsageDate = ko.observableArray([]);
        self.physicalMemoryUsageData = ko.observableArray([]);
        self.physicalMemoryUsageDate = ko.observableArray([]);
        self.nonPagedSystemMemory = ko.observableArray([]);
        self.pagedSystemMemory = ko.observableArray([]);
        self.peakPagedMemory = ko.observableArray([]);
        self.peakVirtualMemory = ko.observableArray([]);

        self.refreshCharts = function () {
            ajax.get('/health').done(function (response) {
                if (response.value.length > 0) {
                    self.physicalMemoryUsageDate([]);

                    self.physicalMemoryUsageData([]);
                    self.virtualMemoryUsageData([]);
                    self.nonPagedSystemMemory([]);
                    self.pagedSystemMemory([]);
                    self.peakPagedMemory([]);
                    self.peakVirtualMemory([]);

                    var v = response[0];
                    var p = response[1];
                    var npsm = response[2];
                    var psm = response[3];
                    var ppm = response[4];
                    var pvm = response[5];

                    $.each(v, function (row, val) {
                        self.virtualMemoryUsageDate.push(val.healthCheckDateTimeString);
                        self.virtualMemoryUsageData.push(((val.value) / 1024) / 1024);
                    });

                    $.each(p, function (row, val) {
                        self.physicalMemoryUsageDate.push(val.healthCheckDateTimeString);
                        self.physicalMemoryUsageData.push(((val.value) / 1024) / 1024);
                    });

                    $.each(npsm, function (row, val) {
                        self.nonPagedSystemMemory.push(((val.value) / 1024) / 1024);
                    });
                    
                    $.each(psm, function (row, val) {
                        self.pagedSystemMemory.push(((val.value) / 1024) / 1024);
                    });

                    $.each(ppm, function (row, val) {
                        self.peakPagedMemory.push(((val.value) / 1024) / 1024);
                    });

                    $.each(pvm, function (row, val) {
                        self.peakVirtualMemory.push(((val.value) / 1024) / 1024);
                    });


                    /* var vp = $('#HVM').highcharts();
                    vp.series[0].setData(self.virtualMemoryUsageData(), true);
                    vp.xAxis[0].setCategories(self.virtualMemoryUsageDate(), true);
                 */
                    var pp = $('#PVM').highcharts();
                    pp.series[0].setData(self.physicalMemoryUsageData(), true);
                    pp.series[1].setData(self.virtualMemoryUsageData(), true);
                    pp.series[2].setData(self.nonPagedSystemMemory(), true);
                    pp.series[3].setData(self.pagedSystemMemory(), true);
                    pp.series[4].setData(self.peakPagedMemory(), true);
                    pp.series[5].setData(self.peakVirtualMemory(), true);
                    pp.xAxis[0].setCategories(self.physicalMemoryUsageDate(), true);
                }
            });
        };

        //Highchart Physical memory
        $(function () {
            $('#PVM').highcharts({
                chart: {

                },
                title: {
                    text: ''
                },
                legend: {
                    enabled: false
                },
                xAxis: {
                    type: 'datetime',
                },
                series: [{
                    name: ' Physical Memory Usage',
                    data: []
                }, {
                    name: 'Virtual Memory Usage',
                    data: []
                }, {
                    name: 'Non Paged System Memory',
                    data: []
                }, {
                    name: 'Paged System Memory',
                    data: []
                }, {
                    name: 'Peak Paged Memory',
                    data: []
                }, {
                    name: 'Peak Virtual Memory',
                    data: []
                }]
            });
        });

        //Highchart Virtual memory
        /*   $(function () {
               $('#HVM').highcharts({
                   chart: {
                   },
                   title: {
                       text: ''
                   },
                   legend: {
                       enabled: false
                   },
                   xAxis: {
                       type: 'datetime',
                       categories: []
                   },
   
                   series: [{
                       data: []
                   }]
               });
           });
   */
        self.refreshCharts();
    };

    exports.exec = function (sectionId, model) {
        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});